#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QThread>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serialthread.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    m_TestFlag = false;
    m_SerialIsOpen = false;
    ui->setupUi(this);

    SetupUI();
    InitSerail();
    qDebug() << "WindowsThread id: " << QThread::currentThreadId();

    m_SerialThread = new SerialThread();
    connect(m_SerialThread, &SerialThread::SignalInfo, this, &MainWindow::SlotWriteLog);
    connect(m_SerialThread, &SerialThread::SignalCCID, this, &MainWindow::SlotCCID);
    connect(m_SerialThread, &SerialThread::SignalIMEI, this, &MainWindow::SlotIMEI);
    connect(m_SerialThread, &SerialThread::SignalCSQ, this, &MainWindow::SlotCSQ);
    connect(m_SerialThread, &SerialThread::SignalUpDown, this, &MainWindow::SlotUpDownCount);
    connect(this, &MainWindow::SignalStartThread, m_SerialThread, &SerialThread::SlotRecvCircle);
}

MainWindow::~MainWindow()
{
    m_SerialThread->CloseSerial();
    //等待子线程完成串口关闭,否则容易出错
    QThread::msleep(100);

    disconnect(m_SerialThread, &SerialThread::SignalInfo, this, &MainWindow::SlotWriteLog);
    disconnect(m_SerialThread, &SerialThread::SignalCCID, this, &MainWindow::SlotCCID);
    disconnect(m_SerialThread, &SerialThread::SignalIMEI, this, &MainWindow::SlotIMEI);

    disconnect(this, &MainWindow::SignalStartThread, m_SerialThread, &SerialThread::SlotRecvCircle);
    delete ui;
    delete m_SerialThread;
}

void MainWindow::SetupUI()
{
    this->setWindowTitle(u8"NBIot模组测试");

    ui->RecvNumLabel->setText(GetRecvNumStr(0));
    ui->SndNumLabel->setText(GetSndNumStr(0));
}

void MainWindow::InitSerail()
{
    ui->SerialComboBox->clear();
    ClosePort();
    for(auto &dev:QSerialPortInfo::availablePorts())
    {
        QSerialPort com;

        com.setPort(dev);
        if(com.open(QIODevice::ReadWrite)) {
            ui->SerialComboBox->addItem(com.portName());
            com.close();
        }
    }
}

void MainWindow::ClosePort()
{
    if(m_SerialIsOpen) {
        ui->ComButton->setText(u8"打开串口");
        m_SerialThread->CloseSerial();

        m_SerialIsOpen = false;
    }
}

void MainWindow::on_ComButton_clicked()
{
    if(m_SerialIsOpen) {
        ClosePort();
    }
    else {
        QString port = ui->SerialComboBox->currentText();
        if(!port.isEmpty()) {
            m_SerialThread->OpenSerial(port);
            QThread::msleep(100);
            m_SerialIsOpen = m_SerialThread->IsOpen();
            if(m_SerialIsOpen)
                ui->ComButton->setText(u8"关闭串口");
        }
    }
}

void MainWindow::on_OpenBtn_clicked()
{
    if(m_TestFlag) {
        ui->SerialComboBox->setDisabled(false);
        ui->ComButton->setDisabled(false);
        ui->SerialRefreshBtn->setDisabled(false);

        ui->OpenBtn->setText(u8"开始调试");
        m_SerialThread->SetRun(false);
    }
    else {
        ui->SerialComboBox->setDisabled(true);
        ui->ComButton->setDisabled(true);
        ui->SerialRefreshBtn->setDisabled(true);

        ui->OpenBtn->setText(u8"停止调试");
        m_SerialThread->SetRun(true);

        //emit SignalStartThread();
    }
    m_TestFlag = !m_TestFlag;
}

void MainWindow::on_SerialRefreshBtn_clicked()
{
    InitSerail();
}

void MainWindow::SlotWriteLog(const QByteArray &str)
{
    QString sstr = QString(str);

    sstr = sstr.trimmed();
    ui->LogplainTxt->appendPlainText(sstr);
    ui->RecvNumLabel->setText(GetRecvNumStr(m_SerialThread->GetRecvNum()));
    ui->SndNumLabel->setText(GetSndNumStr(m_SerialThread->GetSndNum()));
}

void MainWindow::SlotCCID(const QString &ccid)
{
    QString tmp = QString("CCID:%1").arg(ccid);

    tmp = tmp.trimmed();
    ui->CCIDlabel->setText(tmp);
}

void MainWindow::SlotIMEI(const QString &imei)
{
    QString tmp = QString("IMEI:%1").arg(imei);

    tmp = tmp.trimmed();
    ui->IMEIlabel->setText(tmp);
}

void MainWindow::SlotCSQ(const QString &csq)
{
    QString tmp = QString("CSQ:%1").arg(csq);

    tmp = tmp.trimmed();
    ui->CSQlabel->setText(tmp);
}

void MainWindow::on_ClrLogBtn_clicked()
{
    ui->LogplainTxt->clear();
}

void MainWindow::on_ClrNumBtn_clicked()
{
    ui->RecvNumLabel->setText(GetRecvNumStr(0));
    ui->SndNumLabel->setText(GetSndNumStr(0));
    m_SerialThread->ClrRecvSndNum();
}

QString MainWindow::GetRecvNumStr(size_t n)
{
    QString str = QString("Recv:%1").arg(n, 6);

    return str;
}

QString MainWindow::GetSndNumStr(size_t n)
{
    QString str = QString("Snd:%1").arg(n, 6);

    return str;
}

void MainWindow::on_ClrUpDownBtn_clicked()
{
    mUpCnt = mDownCnt = 0;
    ui->UpLineEdit->clear();
    ui->DownLineEdit->clear();
}

void MainWindow::SlotUpDownCount(bool type)
{
    if(type) {
        mUpCnt++;
        ui->UpLineEdit->setText(QString::number(mUpCnt));
    }
    else {
        mDownCnt++;
        ui->DownLineEdit->setText(QString::number(mDownCnt));
    }
}
